﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Strom.Core.Accounts;

namespace Strom.Core.Database
{
    public class AccountsManager
    {
        private readonly BinaryDatabase _database;

        public AccountsManager(BinaryDatabase database)
        {
            _database = database;
        }

        public bool AccountExists(string login)
        {
            lock (_database.AccountsList)
            {
                return !_database.AccountsList.TrueForAll(current => current.Infos.Login != login);
            }
        }

        public Account GetAccountByLogin(string login)
        {
            lock (_database.AccountsList)
            {
                return _database.AccountsList.Find(current => current.Infos.Login == login);
            }
        }

        public bool TryToLogin(string login, string encryptedPassword)
        {
            if (!AccountExists(login))
                return false;

            Account acc = GetAccountByLogin(login);

            return acc.Infos.MD5Password == encryptedPassword && !acc.IsOnline;
        }
    }
}
